package com.atguigu.system.controller;


import com.atguigu.common.result.Result;
import com.atguigu.system.model.entity.SupplierManagement;
import com.atguigu.system.service.SupplierManagementService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  供应商管理
 * </p>
 *
 * @author 
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/system/supplier-management")
public class SupplierManagementController {


    @Autowired
    private SupplierManagementService bizSupplierManagementService;

    // 新增供应商
    @PostMapping("/save")
    public Result<?> saveSupplier(@RequestBody SupplierManagement supplier) {
        boolean save = bizSupplierManagementService.save(supplier);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail("新增供应商失败");
        }
    }

    // 根据 ID 删除供应商
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteSupplier(@PathVariable Integer id) {
        boolean remove = bizSupplierManagementService.removeById(id);
        if (remove) {
            return Result.ok();
        } else {
            return Result.fail("删除供应商失败");
        }
    }

    // 根据 ID 更新供应商信息
    @PutMapping("/update")
    public Result<?> updateSupplier(@RequestBody SupplierManagement supplier) {
        boolean update = bizSupplierManagementService.updateById(supplier);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail("更新供应商信息失败");
        }
    }

    // 根据 ID 查询供应商信息
    @GetMapping("/get/{id}")
    public Result<?> getSupplier(@PathVariable Integer id) {
        SupplierManagement supplier = bizSupplierManagementService.getById(id);
        if (supplier != null) {
            return Result.ok(supplier);
        } else {
            return Result.fail("未找到该供应商信息");
        }
    }

    // 根据 ID 查询供应商信息
    @GetMapping("/list")
    public Result<?> getSupplier() {
        List<SupplierManagement> list = bizSupplierManagementService.list();
        if (list != null) {
            return Result.ok(list);
        } else {
            return Result.fail("未找到该供应商信息");
        }
    }

    // 分页查询供应商信息
    @GetMapping("/page")
    public Result<?> pageQuery(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(required = false) String supplierName,
                               @RequestParam(required = false) String cooperationStatus,
                               @RequestParam(required = false) String contactPhone){
        Page<SupplierManagement> page = new Page<>(pageNum, pageSize);
        Page<SupplierManagement> resultPage = bizSupplierManagementService.pageQuery(page,supplierName,cooperationStatus,contactPhone);
        return Result.ok(resultPage);
    }

}

